package io.renren.util;

import cn.hutool.core.util.HexUtil;
import lombok.extern.slf4j.Slf4j;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class CommonUtil {
  /**
   * 将字符串2位一组前后调换
   * 如入参：123456  返回 563412
   * @param s
   * @return
   */
  public static String reverseString(String s) {
    List<String> stringList = new ArrayList<>();
    for (int i = 0; i < s.length() - 1; i = i + 2) {
      stringList.add(s.substring(i, i + 2));
    }
    Collections.reverse(stringList);
    return stringList.stream().collect(Collectors.joining());
  }

  public static String int2HexString(int value,int length) {
    byte[] valueBytes = HexUtils.intTo2Byte(value);

    byte[] bytes = new byte[length];
    for (int i = 0; i < length; i++) {
      bytes[i] = (byte) 0x33;
    }
    for (int i = 0; i < valueBytes.length; i++) {
      bytes[length - 1 - i] = (byte) (bytes[length - 1 - i] + valueBytes[i]);
    }
    return reverseString(HexUtils.bytesToHexString(bytes));
  }

  public static String formatId(int id) {
    String idStr = String.valueOf(id);
    for (int i = idStr.length(); i < 3; i++) {
      idStr = "0" + idStr;
    }
    return idStr;
  }
  public static int getRandomNum() {
    return (int) (Math.random() * (80000000)) + 10000000;
  }
  public static Cipher getCipher(int mode){
    try {
      Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
      byte[] key = HexUtils.hexStr2Bytes("0102030405060708090a0b0c0d0e0f10");
      SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
      IvParameterSpec iv = new IvParameterSpec(key);//使用CBC模式，需要一个向量iv，可增加加密算法的强度
      cipher.init(mode, skeySpec, iv);
      return cipher;
    } catch (Exception e) {
      log.error(e.getMessage(), e);
    }
    return null;
  }
  public static void main(String[] args) {
    Cipher cipher = getCipher(Cipher.DECRYPT_MODE);
    try {
      byte[] bytes = cipher.doFinal(HexUtils.hexStr2Bytes("8d80db2e2085cadb5fa68fdd664de4338908c707d9b808d23040df70ac3d0cde23137e62c20c4cffa82161f3ab829c37de90ba36e22f43b7ad5d99779f9d031b73c31e80d2a4a4efe0d78cfdc812fb3746b8dfcbfdf99ee4fc0d4296c45794fd1e9a6e87fca05bbdb291fa05736e80b6eb30e54e824cc64c3307adcbf16fc6075461ac6f511e90b0dcf1dedee8fd97c7277e5538f211837e621858686a77b429a0c0b29592143a690597ec22eac60a6b79bb67d01e9aac9b9ebeaa5c9644e28f733a1d1dd8e8f714cd99fcbe5ece33782c9df13cc83c6e3e30fabad793bf68a23d1e0787cda2e11f32b7079a492b7b617dea6f296bbdbeb203d4ec9f60f15f317baa8b8661457a23003da89223146ee5880888e5a0529611e2d96e488d3a9ec2272acb587235413ed820c1ff251393a44396194f9c809ba9c016f7de359637a15bd92004b94754ec98c561542fc3fadca23104b76703222cff6140eb819c4c18102ad4f4ec5492e7e76422ed96a80ffcd4575cf35867f0ee33ece5cbbd608511e0888f4535774f66966d7e758b2502003995846e72092ea35597eb327d16b3af90ceed26daa9997cea39d74cdba29f0fdddc32f171f7c451af1da82149c58d9c84e80a09374143a1a94233939ea9308d95740fff6c664a185ecd4c4a5eb5dbdd6eeecfa44f7ebef44681a64daaeb2e16102c1a3ee7474186eadc8ae5008c7ea0d8b064170e82fa64b41e63fd1c27d3efb42a4db3f099bc4d66362d4fb3f78245d3dab8d8ced404135f58c3605388f814a6481a438ac9bbd9f2bbc5f6253e4b5a78b38303b026c251e2c346c845f46f15ef7f9bd03d2218b644abd219d52b0afbfab423af7ef5c8b5b25780a5152c7f3c"));
      System.out.println(new String(bytes,"GB2312"));
//      String s = "中撒";
//      System.out.println(new String(s.getBytes("GB2312"),"GB2312"));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
